<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Form Validation in JavaScript Example</title>
        <style>
            :root {
                --succes-color: #2ecc71;;
                --error-color: #e74c3c;
            }

            .container {
                background-color: #fff;
                border-radius: 5px;
                box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
                width: 400px;
                margin: 10px auto;
            }

            h2 {
                text-align: center;
                margin: 0 0 20px;
            }

            .form {
                padding: 30px 40px;
            }

            .form-control {
                margin-bottom: 10px;
                padding-bottom: 20px;
                position: relative;
            }

            .form-control label {
                color: #777;
                display: block;
                margin-bottom: 5px;
            }

            .form-control input {
                border: 2px solid #f0f0f0;
                border-radius: 4px;
                display: block;
                width: 100%;
                padding: 10px;
                font-size: 14px;
            }

            .form-control input:focus {
                outline: 0;
                border-color: #777;

            }

            .form-control.success input {
                border-color: var(--succes-color);
            }

            .form-control.error input {
                border-color: var(--error-color);
            }

            .form-control small {
                color: var(--error-color);
                position: absolute;
                bottom: 0;
                left: 0;
                visibility: hidden;
            }

            .form-control.error small {
                visibility: visible;
            }

            .form button {
                cursor: pointer;
                background-color: #3498db;
                border: 2px solid #3498db;
                border-radius: 4px;
                color: #fff;
                display: block;
                padding: 10px;
                font-size: 16px;
                margin-top: 20px;
                width: 100%;
            }

        </style>
        <script type="text/javascript" src="./js/jQuery-1.0.0.js"></script>
    </head>

    <script type="text/javascript">
        $(function () {
            $("#username").blur(function () {
                $.ajax({
                    type: "get",
                    url: "${pageContext.request.contextPath}/user/checkRegister",
                    data: "t=" + new Date().getTime() + "&username=" + this.value,
                    success: function (json) {
                        document.getElementById("check").innerHTML = json.msg
                    }
                })
            })
        })
    </script>

    <body>
        <div class="container">
            <form id="form" action="${pageContext.request.contextPath}/user/register" method="post" class="form">
                <h2>Register</h2>
                <div class="form-control">
                    <label for="username">Username</label>
                    <input type="text" id="username" name="username">
                    <span id="check"></span>
                </div>
                <div class="form-control">
                    <label for="password">Password</label>
                    <input type="password" id="password" name="password">
                    <small>Error Message</small>
                </div>
                <button>注册</button>
            </form>
        </div>
    </body>
</html>